package com.jr.mapper;

import com.jr.pojo.Notification;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

@Component
public interface NotificationMapper {

    @Insert("INSERT INTO notifications (user_id, title, content, type, is_read, created_at) " +
            "VALUES (#{userId}, #{title}, #{content}, #{type}, #{isRead}, #{createdAt})")
    int insertNotification(Notification notification);

    @Select("SELECT notification_id as notificationId, user_id as userId, title, content, type, is_read as isRead, created_at as createdAt " +
            "FROM notifications WHERE user_id = #{userId} ORDER BY created_at DESC")
    ArrayList<Notification> selectByUserId(Integer userId);

    @Select("SELECT notification_id as notificationId, user_id as userId, title, content, type, is_read as isRead, created_at as createdAt " +
            "FROM notifications WHERE user_id = #{userId} ORDER BY created_at DESC LIMIT #{offset}, #{pageSize}")
    ArrayList<Notification> selectByUserIdWithPagination(@Param("userId") Integer userId, @Param("offset") Integer offset, @Param("pageSize") Integer pageSize);

    @Select("SELECT COUNT(*) FROM notifications WHERE user_id = #{userId}")
    int selectCountByUserId(Integer userId);

    @Update("UPDATE notifications SET is_read = #{isRead} WHERE notification_id = #{notificationId}")
    int updateReadStatus(@Param("notificationId") Integer notificationId, @Param("isRead") Boolean isRead);

    @Select("SELECT COUNT(*) FROM notifications WHERE user_id = #{userId} AND is_read = false")
    int selectUnreadCount(Integer userId);

    @Delete("DELETE FROM notifications WHERE notification_id = #{notificationId}")
    int deleteNotification(Integer notificationId);
}
